//
//  SHYSchemaParseHelper.m
//  Pods
//
//  Created by sunhaiyan on 16/6/17.
//
//

#import "SHYSchemaParseHelper.h"
#import "SHYSchemaRouterNodeHeader.h"
#import "SHYSchemaRouterNode.h"
#import "NSDictionary+URL.h"
#import "SHYValidJudge.h"

@implementation SHYSchemaParseHelper

+ (NSString *)schemaUrlStringForSchemaRooterNode:(SHYSchemaRouterNode *)node {

    NSString *urlString = [NSString stringWithFormat:@"%@://%@/%@?%@",SHYSchemaRouterNodeHeaderPrefixThisApp,node.schemaTabName,node.schemaModuleName,[node.paramDict URLQueryString]];
    return urlString;
}

+ (NSURL *)schemaUrlForSchemaRooterNode:(SHYSchemaRouterNode *)node {
    return [NSURL URLWithString:[self schemaUrlStringForSchemaRooterNode:node]];
}

+ (NSString *)schemaUrlStringHeaderForSchemaRooterNode:(SHYSchemaRouterNode *)node {
    NSString *urlString = [NSString stringWithFormat:@"%@://%@/%@",SHYSchemaRouterNodeHeaderPrefixThisApp,node.schemaTabName,node.schemaModuleName];
    return urlString;
}

+ (NSArray *)getSchemaUrlArrayFromParseUrl:(NSURL *)url {
    NSString *urlString = url.absoluteString;
    
    if (url && [SHYValidJudge isValidString:urlString]) {
        if ([[urlString lowercaseString] hasPrefix:[SHYSchemaRouterNodeHeaderPrefixThisApp lowercaseString]]) {
            // 只有prefix是当前APP的头，才能应用内部跳转
            NSArray *urlStrArr =  [urlString componentsSeparatedByString:@"?"];
            if (urlStrArr.count == 2) {
                // 去掉附加信息的部分就是缓存的key，直接获取node数据；
                return urlStrArr;
            }
        }
    }
    return nil;
}

@end
